package EmailProcess;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;

public class EmailAttachment {
    {
        try{
            String SMTP_HOST_NAME = "smtp.gmail.com";
            int SMTP_HOST_PORT = 465;

            String SMTP_AUTH_USER = "jasonchi.cloud@gmail.com";
            String SMTP_AUTH_PWD = "snapple3873";
//            String SMTP_MailTo = "tcrewgh28@gmail.com";
            String SMTP_MailTo = "jasonchi.acs@gmail.com";
            String SMTP_Subject = "!NEW! POAnalysis Report (Automated) Do not reply";
            String SMTP_Body = "This is an Auto-generated POAnalysis Report based on 3 Days history, after data exchange between 2 sites, include new items and CIF calculation";

            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtps");
            props.put("mail.smtps.host", SMTP_HOST_NAME);
            props.put("mail.smtps.auth", "false");
            props.put("mail.smtps.quitwait", "false");
            Session mailSession = Session.getDefaultInstance(props);
            //mailSession.setDebug(true);      //disable/enable onscreen progress
            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject(SMTP_Subject);
            message.setContent(SMTP_Body, "text"); // text/html

            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("PoAnalysis Report");
            MimeMultipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(new File("c:\\windows\\workfolder\\poanalysis.zip"));
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("PoAnalysis.zip");
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(SMTP_MailTo));
            transport.connect
                    (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
            transport.sendMessage(message,
                    message.getRecipients(Message.RecipientType.TO));
            transport.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
